package com.kingdee.eas.custom.hr.attendance.app;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.person.PersonCollection;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.hr.attendance.CalendarTypeInfo;
import com.kingdee.eas.custom.hr.attendance.PersonScheduleFactory;
import com.kingdee.eas.custom.hr.attendance.PersonScheduleInfo;
import com.kingdee.eas.custom.hr.attendance.SchedulDetailCollection;
import com.kingdee.eas.custom.hr.attendance.SchedulDetailFactory;
import com.kingdee.eas.custom.hr.attendance.SchedulDetailInfo;
import com.kingdee.eas.custom.hr.attendance.WorkforceManagementCollection;
import com.kingdee.eas.custom.hr.attendance.WorkforceManagementFactory;
import com.kingdee.eas.custom.hr.attendance.WorkforceManagementInfo;
import com.kingdee.eas.custom.hr.attendance.WorkingTimeZoneFlucationEntryInfo;
import com.kingdee.eas.custom.hr.attendance.WorkingTimeZoneMgrInfo;
import com.kingdee.eas.framework.CoreBaseCollection;

public class WorkForceMgrFacadeControllerBean extends
		AbstractWorkForceMgrFacadeControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.hr.attendance.app.WorkForceMgrFacadeControllerBean");

	@Override
	protected void _generateSchedule(Context ctx) throws BOSException,
			EASBizException {

		int year = Calendar.getInstance().get(Calendar.YEAR);
		// year = year + 1;

		WorkforceManagementCollection wfmCollection = WorkforceManagementFactory
				.getLocalInstance(ctx).getWorkforceManagementCollection(
						"where applyYear=" + year);

		Map<String, WorkforceManagementInfo> map = new HashMap<String, WorkforceManagementInfo>();
		for (int i = 0; i < wfmCollection.size(); i++) {
			map.put(wfmCollection.get(i).getNumber(), wfmCollection.get(i));
		}

		EntityViewInfo view = new EntityViewInfo();
		SelectorItemCollection selectors = new SelectorItemCollection();
		selectors.add("id");
		selectors.add("number");
		selectors.add("name");
		selectors.add("workforcemanagement.*");
		selectors.add("workGroup");
		view.setSelector(selectors);

		// 00000000-0000-0000-0000-0000000000035C14FC3A
		FilterInfo fi = new FilterInfo();
		fi.getFilterItems().add(
				new FilterItemInfo("employeeType.employeeFenLei.id",
						"00000000-0000-0000-0000-0000000000035C14FC3A",
						CompareType.NOTEQUALS));

		// fi.getFilterItems().add(
		// new FilterItemInfo("dept.id", "g7vIDjRMTeqdddS2IHgVSsznrtQ=",
		// CompareType.EQUALS));

		// fi.setMaskString("#0 and #1");

		view.setFilter(fi);
		PersonCollection pers = PersonFactory.getLocalInstance(ctx)
				.getPersonCollection(view);

		Map<String, SchedulDetailCollection> wfms = new HashMap<String, SchedulDetailCollection>();
		SchedulDetailCollection schedules = null;

		CoreBaseCollection perCollection = new CoreBaseCollection();
		CoreBaseCollection personScheduleCollection = new CoreBaseCollection();

		for (int i = 0; i < pers.size(); i++) {

			PersonInfo p = pers.get(i);

			WorkforceManagementInfo wfm = p.getWorkforceManagement();
			String grp = p.getWorkGroup();
			if (wfm == null || grp == null || grp.equals("")) {
				continue;
			}

			String wfmNum = wfm.getNumber();
			String temp = wfmNum.split("-")[1];
			String newWfmNum = year + "-" + temp;
			String key = newWfmNum + "-" + grp;

			if (wfms.containsKey(key)) {
				schedules = wfms.get(key);
			} else {

				if (map.get(newWfmNum) == null) {
					continue;
				} else {
					schedules = SchedulDetailFactory.getLocalInstance(ctx)
							.getSchedulDetailCollection(
									"where workforceManagement.id='"
											+ map.get(newWfmNum).getId()
													.toString()
											+ "' and group='" + grp + "'");

					wfms.put(key, schedules);
				}
			}

			if (schedules != null && schedules.size() >= 0) {
				this.generateScheduleTable(ctx, p, schedules);

				selectors = new SelectorItemCollection();
				selectors.add("workforceManagement");

				p.setWorkforceManagement(map.get(newWfmNum));

				perCollection.add(p);

				logger.info("var:" + (i + 1) + " in " + pers.size() + ":["
						+ p.getNumber() + "," + p.getName() + "," + wfmNum
						+ "," + newWfmNum + "]");
				System.out.println("var:" + (i + 1) + " in " + pers.size()
						+ ":[" + p.getNumber() + "," + p.getName() + ","
						+ wfmNum + "," + newWfmNum + "]");
			}
		}
		PersonFactory.getLocalInstance(ctx).updateBatchData(perCollection);
	}

	private CoreBaseCollection generateScheduleTable(Context ctx,
			PersonInfo personInfo, SchedulDetailCollection schedules)
			throws BOSException, EASBizException {

		Calendar c = Calendar.getInstance();

		int currentYear = c.get(Calendar.YEAR);
		// currentYear = currentYear + 1;
		c.set(Calendar.YEAR, currentYear);

		// String beginDate = String.valueOf(currentYear) + "-01-01";

		CoreBaseCollection personScheduleCollection = new CoreBaseCollection();
		SchedulDetailInfo schedule = null;
		for (int i = 0; i < schedules.size(); i++) {
			schedule = schedules.get(i);
			String days = schedule.getDays();
			String month = schedule.getMonth();
			String[] dayArrs = days.split(",");

			for (int j = 0; j < dayArrs.length; j++) {
				String[] day = dayArrs[j].split(":", 2);
				c.set(Calendar.MONTH, Integer.parseInt(month) - 1);
				c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day[0]));

				if (day[1] != null && !day[1].equals("")) {
					PersonScheduleInfo personScheduleInfo = new PersonScheduleInfo();
					if (BOSUuid.read(day[1]).getType().toString().equals(
							"1F8115DE")) {
						CalendarTypeInfo rest = new CalendarTypeInfo();
						rest.setId(BOSUuid.read(day[1]));
						personScheduleInfo.setRest(rest);
					} else if (BOSUuid.read(day[1]).getType().toString()
							.equals("67912D14")) {
						WorkingTimeZoneMgrInfo workingTime = new WorkingTimeZoneMgrInfo();
						workingTime.setId(BOSUuid.read(day[1]));
						personScheduleInfo.setWorkTimeZone(workingTime);
					}
					personScheduleInfo.setScheduleYear(currentYear);
					personScheduleInfo.setPerson(personInfo);
					personScheduleInfo.setScheduleDate(c.getTime());

					personScheduleCollection.add(personScheduleInfo);

				}
			}
		}

		PersonScheduleFactory.getLocalInstance(ctx).save(
				personScheduleCollection);
		return personScheduleCollection;

	}
}